<template>
  <section>
    <!--工具条：关键字查询 + 新增-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getTypes">关键字</el-button>
        </el-form-item>
        <el-form-item>
          <!--v-perm="'menu:add'"-->
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表：优秀实践，copy domain修改列表-->
    <el-table  :data="types" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              @row-dblclick="showDetailsTable"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
<!--      <el-table-column type="index" width="60">
      </el-table-column>-->
      <el-table-column prop="sn" label="TypeName" sortable>
      </el-table-column>
      <el-table-column prop="name" label="类型名称" sortable>
      </el-table-column>
      <el-table-column label="操作" min-width="120">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <!-- v-perm="'menu:delete'" -->
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
                     :page-sizes="[5, 10, 20, 30]" :page-size="pageSize" :total="totals"  style="float:right;">
      </el-pagination>
    </el-col>
    <!--  数据字典明细表格  -->
    <el-dialog :visible.sync="detailFormVisible" :close-on-click-modal="false">
      <!--工具条：关键字查询 + 新增-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true">
          <el-form-item>
            <el-input v-model="keyword2" placeholder="关键字"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-on:click="getDetails">关键字</el-button>
          </el-form-item>
          <el-form-item>
            <!--v-perm="'menu:add'"-->
            <el-button type="primary" @click="handleAdd2">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>

      <el-table :data="details" highlight-current-row v-loading="listLoading" @selection-change="selsChange2"
                style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="id" label="id" sortable>
        </el-table-column>
        <el-table-column prop="name" label="明细名称" sortable>
        </el-table-column>
        <el-table-column label="操作" min-width="120">
          <template slot-scope="scope">
            <el-button size="small" @click="handleEdit2(scope.$index, scope.row)">编辑</el-button>
            <!-- v-perm="'menu:delete'" -->
            <el-button type="danger" size="small" @click="handleDel2(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--工具条-->
      <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="batchRemove2" :disabled="this.sels2.length===0">批量删除</el-button>
        <el-pagination layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange2" @current-change="handleCurrentChange2" :current-page="currentPage2"
                       :page-sizes="[5, 10, 20, 30]" :page-size="pageSize2" :total="totals2"  style="float:right;">
        </el-pagination>
      </el-col>
    </el-dialog>
    <!--字典明细新增和编辑对话框-->
    <el-dialog :title="title2" :visible.sync="saveFormVisible2" :close-on-click-modal="false">
      <!--ref="menuForm" 指定表单名称，以后可以通过this.$refs.menuForm-->
      <el-form :model="saveForm2" label-width="80px" :rules="saveFormRules2" ref="saveForm2">
        <el-form-item label="明细名称" prop="name">
          <el-input v-model="saveForm2.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible2 = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit2" :loading="saveLoading">保存</el-button>
      </div>
    </el-dialog>

    <!--新增和编辑对话框-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <!--ref="menuForm" 指定表单名称，以后可以通过this.$refs.menuForm-->
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="TypeName" prop="sn">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">保存</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
export default {
  data() {
    return {
      //分页显示所属数据
      types: [],
      details:[],
      keyword2:"",
      listLoading: false,
      currentPage: 1,
      currentPage2: 1,
      pageSize: 10,
      pageSize2: 10,
      totals: 0,
      totals2: 0,

      //高级查询所需数据
      keyword: '',

      //多条数据选中
      sels: [],//列表选中列
      sels2: [],


      //添加或修改数据

      saveFormVisible: false,//添加或修改界面是否显示
      saveFormVisible2: false,
      detailFormVisible : false,
      saveLoading: false,
      title: null,
      title2: null,
      menuTree: [],
      saveFormRules: {
        name: [
          {required: true, message: '请输入名称', trigger: 'blur'}
        ],
      },
      saveFormRules2: {
        name: [
          {required: true, message: '请输入名称', trigger: 'blur'}
        ],
      },

      //编辑界面数据
      saveForm: {
        sn: '',
        name: '',
      },
      saveForm2:{
        id:'',
        name: '',
        typesId:''
      }
    }
  },
  methods: {
    showDetailsTable(row){
      this.detailFormVisible = true;
      this.typesId = row.id;
      this.getDetails(this.typesId);
    },

    getDetails(id) {
      id=this.typesId;
      let paras = {
        currentPage: this.currentPage2,
        pageSize: this.pageSize2,
        keyword: this.keyword2
      };
      console.log(id)
      this.listLoading = true;
      this.$http.post("/tSystemdictionarydetail/selectByQueryPage/" + id , paras).then(res => {
        this.totals2 = res.data.resultObj.total;
        this.details = res.data.resultObj.list;
        this.listLoading = false;
      }).catch(res => {
        this.$message.error("获取分页数据失败!!!");
      })
    },

    //获取菜单列表
    getTypes() {
      let paras = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.keyword
      };
      this.listLoading = true;
      this.$http.post("/tSystemdictionarytype/selectByQueryPage", paras).then(res => {
        this.totals = res.data.resultObj.total;
        this.types = res.data.resultObj.list;
        this.listLoading = false;
      }).catch(res => {
        this.$message.error("获取分页数据失败!!!");
      })
    },

    //关键字查询
    keywordQuery() {
      this.currentPage = 1;
      this.getTypes();
    },

    handleSizeChange(curentPage) {
      this.pageSize = curentPage
      this.getTypes();
    },
    handleSizeChange2(curentPage){
      this.pageSize2 = curentPage
      this.getDetails(this.typesId)
    },

    //分页查询 - 点击某一页查询
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getTypes();
    },

    handleCurrentChange2(val){
      this.currentPage2 = val;
      this.getDetails(this.typesId)
    },

    //点击排序
    handleChange(value) {
      this.saveForm.index = value;
    },


    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/tSystemdictionarytype/delete/" + row.id).then(res => {
          let {success} = res.data;
          //如果成功
          if (success) {
            //刷新界面
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            if ((this.totals - 1) % this.pageSize == 0 && this.currentPage != 1) {
              this.currentPage -= 1;
            }
            this.getTypes();
          } else {
            this.$message({
              message: '删除失败',
              type: 'error'
            });
          }
          this.listLoading = false;
        })
      }).catch(() => {

      });
    },

    handleDel2(index, row){
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/tSystemdictionarydetail/delete/" + row.id).then(res => {
          let {success} = res.data;
          //如果成功
          if (success) {
            //刷新界面
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            if ((this.totals2 - 1) % this.pageSize2 == 0 && this.currentPage2 != 1) {
              this.currentPage2 -= 1;
            }
            this.getDetails(this.typesId);
          } else {
            this.$message({
              message: '删除失败',
              type: 'error'
            });
          }
          this.listLoading = false;
        })
      }).catch(() => {

      });
    },


    //点击修改按钮弹出模态框
    handleEdit: function (index, row) {
      this.title = "编辑";
      this.saveForm = Object.assign({}, row);

      //对上级菜单回显做处理：//这里没有dirPath - 无法显示多级部门，只能显示上级部门
      //注意回显的时候：只能选顶级部门，
      this.saveFormVisible = true;
    },

    handleEdit2: function (index, row) {
      this.title = "编辑";
      this.saveForm2 = Object.assign({}, row);

      //对上级菜单回显做处理：//这里没有dirPath - 无法显示多级部门，只能显示上级部门
      //注意回显的时候：只能选顶级部门，
      this.saveFormVisible2 = true;
    },




    //点击添加按钮弹出模态框
    handleAdd: function () {
      this.saveFormVisible = true;
      //修改对话框标题
      this.title = "新增"
      //置空数据，防止先点修改，后点添加有值
      this.saveForm = {
        sn: '',
        name: '',
      };
      //弹出对话框

    },



    handleAdd2: function (){
      this.saveFormVisible2 = true;
      //修改对话框标题
      this.title2 = "新增"
      //置空数据，防止先点修改，后点添加有值
      this.saveForm2 = {
        id: '',
        name: '',
        typesId:''
      };
      //弹出对话框
    },

    //点击模态框提交数据
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;
            this.$http.put("/tSystemdictionarytype/save", this.saveForm).then(res => {
              let {success} = res.data;
              if (success) {
                //刷新界面
                this.getTypes();
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                //关闭加载框
                this.addLoading = false;
                //关闭模态框
                this.saveFormVisible = false;
              } else {
                this.$message({
                  message: '提交失败',
                  type: 'error'
                });
              }
            })
          });
        }
      });
    },

    saveSubmit2: function () {
      console.log(this.typesId);
      this.saveForm2.typesId = this.typesId;
      this.$refs.saveForm2.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;
            this.$http.put("/tSystemdictionarydetail/save", this.saveForm2).then(res => {
              let {success} = res.data;
              if (success) {
                //刷新界面
                this.getDetails(this.typesId);
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                //关闭加载框
                this.addLoading = false;
                //关闭模态框
                this.saveFormVisible2 = false;
              } else {
                this.$message({
                  message: '提交失败',
                  type: 'error'
                });
              }
            })
          });
        }
      });
    },

    selsChange: function (sels) {
      this.sels = sels;
    },

    selsChange2: function (sels) {
      this.sels2 = sels;
    },

    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/tSystemdictionarytype/patchDelete", ids).then(res => {
          this.listLoading = false;
          if (res.data.success) {
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            if ((this.totals - this.sels.length) % this.pageSize == 0 && this.currentPage != 1) {
              this.currentPage -= 1;
            }
            this.getTypes();
          } else {
            this.$message.error("批量删除失败【500】");
          }
        }).catch(res => {
          this.$message.error("系统错误【400,404】");
        })
      }).catch(() => {
      });
    },

    batchRemove2: function () {
      var ids = this.sels2.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/tSystemdictionarydetail/patchDelete", ids).then(res => {
          this.listLoading = false;
          if (res.data.success) {
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            if ((this.totals2 - this.sels2.length) % this.pageSize2 == 0 && this.currentPage2 != 1) {
              this.currentPage2 -= 1;
            }
            this.getDetails(this.typesId);
          } else {
            this.$message.error("批量删除失败【500】");
          }
        }).catch(res => {
          this.$message.error("系统错误【400,404】");
        })
      }).catch(() => {
      });
    },
  },


  mounted() {
    //分页查询 - 页面一加载查询
    this.getTypes();
  }
}

</script>

<style scoped>

</style>